import { HttpClient, HttpParams } from '@angular/common/http';
import { Component, Injectable, OnInit } from '@angular/core';
import { memberUrl, memberImgUrl, memberroleUrl } from '../../../urlConfig';
import { HttpService } from '../../../service/httpService/http.service';
import * as XLSX from 'xlsx';
import { Router } from '@angular/router';
import { LocalStorageService } from '../../../service/localStorageService/local-storage.service';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-member',
  templateUrl: './member.component.html',
  styleUrls: ['./member.component.styl']
})
export class MemberComponent {
  mapOfExpandData: { [key: string]: boolean } = {};
  memberList: any[] = [];
  listOfData = []; // 存放所有成员信息
  imgpreview:''; // 存放预览图片的地址
  memberImgurl: string = memberImgUrl; // 照片前缀
  constructor(
    public http: HttpService,
    public localStorage: LocalStorageService,
    private message: NzMessageService
  ) {}
  xlsxData: any = []; // 要导出的表单

  ngOnInit(): void {
    this.http.getData(memberUrl).subscribe((event: any) => {
      if (event.code === 200) {
        this.listOfData = event.data;
        console.log(this.listOfData);
      }
    });
  }

  // 修改权限等级
  ressetRole(id: number, username: string, value: string) {
    const memberrole = {
      roleId: id,
      accessToken: this.localStorage.getItem('token').accessToken
    };

    this.http
      .putData(`${memberroleUrl}${username}`, memberrole)
      .subscribe((event: any) => {
        if (event.code === 200) {
          this.listOfData.forEach((currentValue: any, index: number) => {
            if (currentValue.username === username) {
              this.listOfData[index].role = value;
            }
          });
          this.createMessage('success');
        } else if (event.code === 201) {
          this.createMessage1('error');
        }
      });
  }

  // 导出成员数据
  deriveXlsx(value: string) {
    this.http.getData(memberUrl).subscribe((event: any) => {
      if (event.code === 200) {
        event.data.forEach((currentValue: any) => {
          if (currentValue.direction === value) {
            this.xlsxData.push(currentValue);
          } else if ('all' === value) {
            this.xlsxData.push(currentValue);
            // var value = '所有成员信息';
          }
        });

        var ws = XLSX.utils.json_to_sheet(this.xlsxData);

        var wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, 'Presidents');

        XLSX.writeFile(wb, `${value}.xlsx`);
      }
    });
  }

  // 预览图片
  preview(memberImgurl,imgname){
    this.imgpreview = memberImgurl + imgname;
    const element = document.getElementById('imgpreviewbox');
    element.style.display = 'block';
  }
 
  // 关闭预览
  imgicon(){
    const element = document.getElementById('imgpreviewbox');
    element.style.display = 'none';
  }

  createMessage(type: string): void {
    this.message.create(type, '修改成功！');
  }

  createMessage1(type: string): void {
    this.message.create(type, '权限不足，修改失败！');
  }
}
